﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommonLib;
using ElementLib.Interface;
using System.Windows.Forms;
using HalconDotNet;
using System.Collections;
namespace ElementLib
{
    public class PointPack : IElemPack
    {
        private Point point;
        private ROI detectROI;
        private Line lineROI;
        private static int[] existCount = new int[3];
        private int detectMethod;
        private string name;
        private HTuple mode = "all";
        private Hashtable args;
        private bool isSelected = false;
        public delegate void SetSelectedItem(string itemName, string mode, IElemPack iep = null);

        
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public bool IsSelected
        {
            get { return isSelected; }
        }

        public Hashtable Args
        {
            get { return args; }
            set { args = value; }
        }
        public int DetectMethod
        {
            get { return detectMethod; }
            set { detectMethod = value; }
        }
        public void BeTouched(HTuple windowHandle,SetSelectedItem method)
        {
            isSelected = isSelected == false ? true : false;
            if (isSelected)
            {
                method(name,"add",this);
                DispROI(windowHandle,ConstValue.SELECTED_COLOR);
            }
            else
            {
                method(name, "remove", null);
                DispROI(windowHandle, ConstValue.UNSELECTED_COLOR);
            }
        }


     
        public ROI DetectROI
        {
            get { return detectROI; }
            set { if (detectROI != null) detectROI.Dispose(); detectROI=value; }
        }
        public string Mode { set { mode = value; }  get { return mode; } }
        public void Dispose()
        {
            if (point !=null) point.Dispose();
            if (detectROI != null) detectROI.Dispose();
            if (lineROI != null) lineROI.Dispose();
        }
        public PointPack()
        {

        }

        public PointPack(Point point, int detectMethod, Hashtable args, ROI detectROI)
        {
            this.point = new Point(point);
            this.name = point.Name;
            this.detectMethod = detectMethod;
            if(args != null)
            {
                IDictionaryEnumerator ie = args.GetEnumerator();
                this.args = new Hashtable();
                while (ie.MoveNext())
                    this.args.Add(ie.Key, ie.Value);
            }
            this.detectROI = new ROI(detectROI);
        }

        public PointPack(Point point, int detectMethod, Hashtable args, Line lineROI)
        {
            this.point = new Point(point);
            this.name = point.Name;
            this.detectMethod = detectMethod;
            if (args != null)
            {
                IDictionaryEnumerator ie = args.GetEnumerator();
                this.args = new Hashtable();
                while (ie.MoveNext())
                    this.args.Add(ie.Key, ie.Value);
            }
            this.lineROI = new Line(lineROI);
        }


        public PointPack(ROI detectROI, int detectMethod, Hashtable args)
        {
            this.detectROI = new ROI(detectROI);
            this.detectMethod = detectMethod;
            this.point = null;
            this.args = new Hashtable();
            if(args != null)
            {
                IDictionaryEnumerator ie = args.GetEnumerator();
                while (ie.MoveNext())
                    this.args.Add(ie.Key, ie.Value);
            }
           
        }

        public PointPack(PointPack pointPack)
        {
            name = pointPack.name;
            detectMethod = pointPack.detectMethod;
            args = new Hashtable();
            if (pointPack.args != null)
            {
                IDictionaryEnumerator ie = pointPack.args.GetEnumerator();
                while (ie.MoveNext())
                    args.Add(ie.Key, ie.Value);
            }
            if(pointPack.detectROI!=null)detectROI = new ROI(pointPack.detectROI);
            point = new Point(pointPack.point);
            if (pointPack.lineROI != null) lineROI = new Line(pointPack.lineROI);
        }

        public void  DispROI(HTuple windowHandle,string color)
        {
            HOperatorSet.SetLineWidth(windowHandle, 2);
            point.dispROI(windowHandle,color);
        }
        public bool IsDetectType()
        {
            if (detectROI == null&&lineROI == null) return false;
            return true ; 
        }

        public Line LineROI
        {
            get { return lineROI; }
            set { if (lineROI != null) lineROI.Dispose();lineROI = value; }
        }
        public double Row
        {
            get { if(point!=null) return point.Row; return 0; }
        }

        public double Column
        {
            get { if (point != null) return point.Column; return 0; }
        }
        public void ApplyChange(string itemName="")
        {
            // point.ApplyChange();
            //if (P.Name=="")
            //{
            //    name = "POINT" + existCount[0]++;
            //    point.Name = name;
            //}
            //else
            //{
            //    name = "点计划"+ existCount[0]++;
            //    name = point.Name;
            //}
            if (P.Name == null)
            {
                P.Name = "点" + existCount[0]++;
            }
            name = P.Name;
        }
        public void Unload()
        {
            point.Dispose();
            point = null;
        }
        public Point Detect(HObject image, HTuple homMat2D)
        {
           
            HObject actualContour;
           
           
            HTuple minThreshold, maxThreshold;
            if (detectMethod == ConstValue.MEASURE_POINT)
            {
                HTuple startRow, startCol, endRow, endCol;
                //HOperatorSet.AffineTransContourXld(lineROI.LineContours, out actualContour, homMat2D);
                HOperatorSet.AffineTransPixel(homMat2D, lineROI.StartPoint.Row, lineROI.StartPoint.Column, out startRow, out startCol);
                HOperatorSet.AffineTransPixel(homMat2D, lineROI.EndPoint.Row, lineROI.EndPoint.Column, out endRow, out endCol);
               // HOperatorSet.DispObj(actualContour, Utils.hv_ExpDefaultWinHandle);
                //MessageBox.Show("111");

                
                HTuple hv_Mode = (HTuple)args[ConstValue.MODE];
                minThreshold = (HTuple)args[ConstValue.MIN_THRESHOLD];
                maxThreshold = (HTuple)args[ConstValue.MAX_THRESHOLD];

               // HOperatorSet.FitLineContourXld(actualContour, "regression", -1, 0, 5, 1.5, out hv_RowEnd, out hv_ColEnd, out hv_RowStart, out hv_ColStart, out Nr, out Nc, out Dist);
                point = Utils.Get_point_fromLine(image, Utils.hv_ExpDefaultWinHandle, hv_Mode, startRow, startCol, endRow, endCol);
                return point;
   
            }
            else if (detectMethod == ConstValue.MEASURE_POINT_PEAK)
            {
                HOperatorSet.AffineTransContourXld(detectROI.Contours(), out actualContour, homMat2D);
                HOperatorSet.DispObj(actualContour, Utils.hv_ExpDefaultWinHandle);
                MessageBox.Show("111");

                MessageBox.Show("需要补全");
            }

            return null;
        }

        public void Reset(int num)
        {
            for (int i = 0; i < existCount.Length; i++)
            {
                existCount[i] = 0;
            }
        }

        public HObject Contours() 
        {
            if (point == null) return null; 
            return point.Contour;
        }
        public HObject Region() 
        {
            if (point == null) return null;
            return point.Region;
        } 
        public void Serialization()
        {
            MessageBox.Show("需要补全");
        }

        public Point P
        {
            get { return point; }
            set { point = value; }
        }
    }
}
